package com.smarthome.core.housemanager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShBuildingService;
import com.smarthome.core.housemanager.BuildingUnitManagerService;
import com.smarthome.domain.bean.ShBuilding;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

@Service("buildingUnitManagerService")
public class BuildingUnitManagerServiceImpl implements BuildingUnitManagerService {
	
	@Resource(name="shBuildingService")
	private ShBuildingService buildingService;

	@Override
	public DataGrid  getDataGrid(ShBuilding building, PageHelper helper) {
		building.setIsUnit(1);
		return buildingService.getUnitDataGrid(building, helper);
	}

	@Override
	public ShBuilding querybuildingById(Long id) {
		ShBuilding building = buildingService.selectEntryById(id);
		if(building != null && building.getParentId() != null){
			ShBuilding pb = buildingService.selectEntryById(building.getParentId());
			building.setParentName(pb.getName());
		}
		return building;
	}

	@Override
	public int add(ShBuilding building) {
		return buildingService.insert(building);
	}

	@Override
	public int edit(ShBuilding building) {
		ShBuilding saveBuilding = buildingService.selectEntryById(building.getId());
		if(saveBuilding != null){
			BeanUtils.copyProperties(building, saveBuilding);
			 buildingService.saveOrUpdate(saveBuilding);
		}
		
		return 0;
	}

	@Override
	public int delte(Long id) {
		
		return buildingService.deleteByIds(id);
	}

	@Override
	public boolean isRepeat(ShBuilding building) {
		if(building == null) return false;
		ShBuilding qn = new ShBuilding();
		qn.setIsUnit(1);
		qn.setName(building.getName());
		qn.setResidenceId(building.getResidenceId());
		qn.setParentId(building.getParentId());
		List<ShBuilding> buildings = buildingService.selectEntryList(qn);
		if(buildings == null || buildings.size() == 0){
			return false;
		}
		
		for(ShBuilding s : buildings){
			if((s.getName() == null  && building.getName() == null)||s.getName().equals(building.getName())  ){
				if(building.getId() != null && s.getId().equals(building.getId())) continue;
				return true;
			}
		}
		
		return false;
		
	}

	@Override
	public List<ShBuilding> queryBuildingByEntry(ShBuilding building) {
		building.setIsUnit(1);
		List<ShBuilding> bs = buildingService.selectEntryList(building);
		return bs;
	}

	/* (non-Javadoc)
	 * @see com.smarthome.core.housemanager.BuildingUnitManagerService#getComboboxs(java.lang.Long)
	 */
	@Override
	public List<Combobox> getComboboxs(Long parentid) {
		List<Combobox> boxs =new ArrayList<Combobox>();
		if(parentid == null){
			return boxs;
		}
		ShBuilding building = new ShBuilding();
		building.setIsUnit(1);
		building.setParentId(parentid);
		List<ShBuilding> bs = queryBuildingByEntry(building);
		if(null != bs){
			for(ShBuilding b : bs){
				boxs.add(new Combobox(b.getId() +"",b.getName()));
			}
		}
		return boxs;
	}

}
